<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use org\Qiniu;
class Timing extends Base {
    /**
     * 神怼评选
     */
    public function setGodComment(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态;定时器3：未答题的一小时后关闭
        //1-1：自营订单的超时自动取消
        $today = date('Y-m-d H:i:s');
        $time = time();
        $one_hour_ago = date("Y-m-d H:i:s",($time - 1800));
        $where['is_pay'] = 0;
        $where['order_contract'] = ['<>',3];
        $where['o.is_delete'] = 0;
        $where['o.created_at'] = ['<=',$one_hour_ago];
        $field = 'o.order_id,goods_id,num,attr_id,order_contract';
        $res = Db::table('up_self_order')->alias('o')
            ->join('up_self_order_detail od','o.order_id=od.order_id','LEFT')
            ->where($where)
            ->field($field)
            ->select();
//        dump($res);die;
        if($res){
            Db::startTrans();
            try{
                foreach ($res as $k=>$v){
                    if($v['order_contract'] == 1){
                        //1、加上商品的库存
                        Db::table('up_self_goods')->where('good_id',$v['goods_id'])->setInc('good_stock',$v['num']);
                        //2、加上attr的库存
                        Db::table('up_self_goods_attr')->where('attr_id',$v['attr_id'])->setInc('stock',$v['num']);
                    }
                    //增补的不用加了
//                    else{
//                        //1、加上商品的库存
//                        Db::table('up_goods')->where('good_id',$v['goods_id'])->setInc('good_stock',$v['num']);
//                        //2、加上attr的库存
//                        Db::table('up_goods')->where('good_id',$v['goods_id'])->setInc("good_{$v['attr_id']}",$v['num']);
//                    }
                }
                //3、改变订单状态
                Db::table('up_self_order o')->where($where)->setField('is_pay',2);
                Db::commit();
                return 1;
            }catch (\Exception $e) {
                Db::rollback();
                return 2;
            }
        }
    }
    public function dowload() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1516439349'; </script>";
        }else{
           echo "<script> location.href='https://a.app.qq.com/o/simple.jsp?pkgname=com.paixun.fz'; </script>";
        }

    }
}